package com.zpz.framework.zpzoauth.service.impl;

import com.zpz.framework.zpzoauth.common.result.ZpzOauthResultMsg;
import com.zpz.framework.zpzoauth.common.result.ZpzOauthResultStatus;
import com.zpz.framework.zpzoauth.common.utils.ZpzOauthRequestUtil;
import com.zpz.framework.zpzoauth.dao.ZpzOauthDataGroupMapper;
import com.zpz.framework.zpzoauth.pojo.pe.DataOauthGroupPe;
import com.zpz.framework.zpzoauth.pojo.qo.DataOauthGroupQo;
import com.zpz.framework.zpzoauth.service.ZpzOauthDataGroupService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author： changyifan
 * @createDate： 2019/7/22 10:30
 * @updateBy：
 * @updateDate：
 * @description：
 */
@Service
public class ZpzOauthDataGroupServiceImpl implements ZpzOauthDataGroupService {

    private Logger log = LoggerFactory.getLogger(ZpzOauthDataGroupServiceImpl.class);

    @Resource
    private ZpzOauthDataGroupMapper dataOauthGroupMapper;

    /**
     * @name: deleteDataOauthGroup
     * @description:删除数据权限组
     * @returnType: ZpzOauthResultMsg<java.lang.Boolean>
     * @createBy: changyifan
     * @createDate: 2019-07-22 02:49:33
     * @updateBy:
     * @updateDate:
     * @param: [request, dataOauthGroupQo]
     * @throws Exception
     */
    @Override
    public ZpzOauthResultMsg<Boolean> deleteDataOauthGroup(HttpServletRequest request, DataOauthGroupQo dataOauthGroupQo) throws Exception {
        Map<String, String> license = ZpzOauthRequestUtil.getLicense(request);
        if (license==null || license.size()==0){
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.GATE_WAY_10023);
        }
        String ucodeLicense = license.get("ucode");
        if (StringUtils.isBlank(ucodeLicense)) {
            return ZpzOauthResultMsg.customResultStatus(ZpzOauthResultStatus.GATE_WAY_10027);
        }
        List<DataOauthGroupPe> list = dataOauthGroupMapper.selectDataOauthGroup();
        if(list != null && list.size() > 0){
            DataOauthGroupPe dataOauthGroupPe = new DataOauthGroupPe();
            dataOauthGroupPe.setId(dataOauthGroupQo.getId());
            dataOauthGroupPe.setUpdateBy(ucodeLicense);
            dataOauthGroupPe.setUpdateDate(new Date());
            Boolean b = dataOauthGroupMapper.deleteDataOauthGroup(dataOauthGroupPe);
            if(b == true){
                return ZpzOauthResultMsg.okForData(b);
            }
            return ZpzOauthResultMsg.okForData(false);
        }
        return ZpzOauthResultMsg.okForData(null);
    }
}
